const mongoose = require('mongoose')

const roleSchema = new mongoose.Schema({
  name: String, // 角色名称
  desc: String, // 角色描述
  menuCheckedKeys: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Menu'
    }
  ],
  menuHalfCheckedKeys: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Menu'
    }
  ],
  apiCheckedKeys: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Api'
    }
  ],
  apiHalfCheckedKeys: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Api'
    }
  ],
  parent: {
    // 所属组
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Role'
  } // 自关联,角色分组
})

// 角色分组详细,指组成员
roleSchema.virtual('children', {
  localField: '_id',
  ref: 'Role',
  foreignField: 'parent',
  justOne: false
})

roleSchema.set('toObject', { virtuals: true })
roleSchema.set('toJSON', { virtuals: true })

const Role = mongoose.model('Role', roleSchema)

module.exports = Role
